package com.example.alibabavodossservice.vod.mediaAudit;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.vod.model.v20170321.*;
import com.example.alibabavodossservice.vod.mediaAudit.AuditSecurityIpParameter;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import static com.example.alibabavodossservice.vod.InitVODConstants.VOD_CLIENT;


/**
 * desc: 音视频图片审核
 *
 * @author xugenyin
 */
public class Cmd {
    private static final String DEFAULT = "default";

    /**
     * 设置审核安全IP行为
     * 当视频状态为审核中（Checking）或屏蔽（Blocked）状态时，只有来自审核安全IP的请求才可以播放。
     *
     * @param auditSecurityIpParameter 安全审核ip参数
     * @return SetAuditSecurityIpResponse 设置安全审核ip响应数据
     */
    public static SetAuditSecurityIpResponse setAuditSecurityIp(AuditSecurityIpParameter auditSecurityIpParameter) throws Exception {
        SetAuditSecurityIpRequest setAuditSecurityIpRequest = new SetAuditSecurityIpRequest();
        BeanUtils.copyProperties(auditSecurityIpParameter, setAuditSecurityIpRequest);
        return VOD_CLIENT.getAcsResponse(setAuditSecurityIpRequest);
    }

    /**
     * 获取审核安全IP列表
     *
     * @param securityGroupName 安全组名称
     * @return ListAuditSecurityIpResponse 获取审核安全IP列表响应数据
     * @desc: 填default表示获取全部安全组的IP列表。
     */
    public static List<ListAuditSecurityIpResponse.SecurityIp> getAuditSecurityIpList(String securityGroupName) throws Exception {
        ListAuditSecurityIpRequest getAuditSecurityIpListRequest = new ListAuditSecurityIpRequest();
        if (DEFAULT.equals(securityGroupName)) {
            securityGroupName = null;
        }
        getAuditSecurityIpListRequest.setSecurityGroupName(securityGroupName);
        return VOD_CLIENT.getAcsResponse(getAuditSecurityIpListRequest).getSecurityIpList();
    }


    /**
     * 提交音视频AI审核作业
     *
     * @param submitAIMediaAuditJobParameter 提交音视频AI审核作业参数
     * @return SubmitAIMediaAuditJobResponse 提交音视频AI审核作业响应数据
     * @throws Exception 异常
     */
    public static SubmitAIMediaAuditJobResponse submitAIMediaAuditJob(SubmitAIMediaAuditJobParameter submitAIMediaAuditJobParameter) throws Exception {
        SubmitAIMediaAuditJobRequest submitAIMediaAuditJobRequest = new SubmitAIMediaAuditJobRequest();
        BeanUtils.copyProperties(submitAIMediaAuditJobParameter, submitAIMediaAuditJobRequest);
        return VOD_CLIENT.getAcsResponse(submitAIMediaAuditJobRequest);
    }

    /**
     * 提交图片AI审核作业
     *
     * @param submitAIImageAuditJobParameter 提交图片AI审核作业参数
     * @return SubmitAIImageAuditJobResponse 提交图片AI审核作业响应数据
     * @throws Exception 异常
     */
    public static SubmitAIImageAuditJobResponse submitAIImageAuditJob(SubmitAIImageAuditJobParameter submitAIImageAuditJobParameter) throws Exception {
        SubmitAIImageAuditJobRequest submitAIImageAuditJobRequest = new SubmitAIImageAuditJobRequest();
        BeanUtils.copyProperties(submitAIImageAuditJobParameter, submitAIImageAuditJobRequest);
        return VOD_CLIENT.getAcsResponse(submitAIImageAuditJobRequest);
    }

    /**
     * 查询智能审核作业结果
     *
     * @param jobId 作业ID
     * @return GetAIMediaAuditJobResponse 智能审核作业结果
     * @throws Exception 异常
     */
    public static GetAIMediaAuditJobResponse getAIMediaAuditJob(String jobId) throws Exception {
        GetAIMediaAuditJobRequest mediaAuditJobRequest = new GetAIMediaAuditJobRequest();
        mediaAuditJobRequest.setJobId(jobId);
        return VOD_CLIENT.getAcsResponse(mediaAuditJobRequest);
    }

    /**
     * 获取智能审核结果摘要
     *
     * @param submitMediaId 提交智能审核作业的视频ID
     * @return GetMediaAuditResultResponse.MediaAuditResult 智能审核结果摘要
     * @throws Exception 异常
     */
    public static GetMediaAuditResultResponse.MediaAuditResult getAIMediaAuditJobSummary(String submitMediaId) throws Exception {
        GetMediaAuditResultRequest mediaAuditResultRequest = new GetMediaAuditResultRequest();
        mediaAuditResultRequest.setMediaId(submitMediaId);
        GetMediaAuditResultResponse mediaAuditResultResponse = VOD_CLIENT.getAcsResponse(mediaAuditResultRequest);
        return mediaAuditResultResponse.getMediaAuditResult();
    }

    /**
     * 获取智能审核结果详情
     *
     * @param submitMediaId 提交智能审核作业的视频ID
     * @param pageNo        分页页码
     * @return GetMediaAuditResultDetailResponse.MediaAuditResultDetail 智能审核结果详情
     * @throws Exception 异常
     */
    public static GetMediaAuditResultDetailResponse.MediaAuditResultDetail getAIMediaAuditJobDetail(String submitMediaId, int pageNo) throws Exception {
        GetMediaAuditResultDetailRequest mediaAuditResultDetailRequest = new GetMediaAuditResultDetailRequest();
        mediaAuditResultDetailRequest.setMediaId(submitMediaId);
        mediaAuditResultDetailRequest.setPageNo(pageNo);
        GetMediaAuditResultDetailResponse mediaAuditResultDetailResponse = VOD_CLIENT.getAcsResponse(mediaAuditResultDetailRequest);
        return mediaAuditResultDetailResponse.getMediaAuditResultDetail();
    }

    /**
     * 获取智能审核结果时间线
     *
     * @param submitMediaId 提交智能审核作业的视频ID
     * @return GetMediaAuditResultTimelineResponse.MediaAuditResultTimeline 智能审核结果时间线
     * @throws Exception 异常
     */
    public static GetMediaAuditResultTimelineResponse.MediaAuditResultTimeline getAIMediaAuditJobTimeline(String submitMediaId) throws Exception {
        GetMediaAuditResultTimelineRequest mediaAuditResultTimelineRequest = new GetMediaAuditResultTimelineRequest();
        mediaAuditResultTimelineRequest.setMediaId(submitMediaId);
        GetMediaAuditResultTimelineResponse mediaAuditResultTimelineResponse = VOD_CLIENT.getAcsResponse(mediaAuditResultTimelineRequest);
        return mediaAuditResultTimelineResponse.getMediaAuditResultTimeline();
    }

    /**
     * 获取智能音频审核结果详情
     *
     * @param jobId 作业ID
     * @return GetAIMediaAuditJobResponse 智能审核作业结果
     * @throws Exception 异常
     */
    public static GetAIMediaAuditJobResponse getAIMediaAuditJobResult(String jobId) throws Exception {
        GetAIMediaAuditJobRequest mediaAuditJobRequest = new GetAIMediaAuditJobRequest();
        mediaAuditJobRequest.setJobId(jobId);
        return VOD_CLIENT.getAcsResponse(mediaAuditJobRequest);
    }

    /**
     * 构建人工审核内容结果
     *
     * @param resultJson 人工审核内容结果
     *                   视频ID
     *                   auditContent.put("VideoId", "3ebc10160bda481ca9b6858a0b58****");
     *                   审核状态
     *                   auditContent.put("Status", "Blocked");
     *                   若审核状态为屏蔽时，需给出屏蔽的理由，最长支持128字节
     *                   auditContent.put("Reason", "含有淫秽色情内容");
     * @return String 人工审核内容结果
     */
    public static String buildAuditContent(JSONObject resultJson) throws Exception {
        List<JSONObject> auditContents = new ArrayList<>();
        auditContents.add(resultJson);
        return auditContents.toString();
    }

    /**
     * 发出人工审核
     */
    public static CreateAuditResponse createAudit(JSONObject resultJson) throws Exception {
        CreateAuditRequest createAuditRequest = new CreateAuditRequest();
        // 设置审核内容
        createAuditRequest.setAuditContent(buildAuditContent(resultJson));
        // 返回结果
        return VOD_CLIENT.getAcsResponse(createAuditRequest);
    }

}
